iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 18
0
自我挑戰組

Women in IT field系列 第 18

Quick review of C++

  • 分享至 

  • xImage
  •  

C++ is the one start to have object idea’s programming.

#include
#include
using namespace std;

int main()
{
int a=0,b=0,c;
cout << "Enter number a: "; cin >> a;
cout<<endl;
cout << "Enter number b: "; cin >> b;
cout<<endl;

c = a&b;
cout<<"Result of & : "<<c<<endl;
c = a|b;
cout<<"Result of | : "<<c<<endl;
c = a^b;
cout<<"Result of ^ : "<<c<<endl;

c = a<<1;
cout<<"Result of << by 2 bits : "<<c<<endl;
c = b>>1;
cout<<"Result of >> by 2 bits : "<<c<<endl;

c = ~3;
cout<<"Result of ~ : "<<c<<endl;
}

example :
a=1
0001
b=2
0010
a&b = 0000
a|b = 0011
a^b = 0011
a<<1 = 0010 =2
b>>1 = 0001 =1
c = -4

#include
#include
using namespace std;

void swap(int* a, int* b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int main()
{
int a, b;
cout<<"Enter 2 values with space in between: "; cin>>a>>b;
cout<<endl;
swap(&a,&b);
cout<<"Swapped values"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b;

return 0;
}

#include
#include
using namespace std;

int main ()
{
char data[50];

ofstream out;
out.open("D:\sample1.txt");
cout << "Writing to a file" << endl;
cout << "Enter your massage: ";
cin.getline(data, 50);
out << data << endl;
cout << "Enter your date (mmddyyyy): "; cin >> data;
cin.ignore();
out << data << endl;
out.close();

ifstream in;
in.open("D:\sample1.txt");
cout << "Reading from a file" << endl;
in >> data;
cout << data << endl;
in >> data;
cout << data << endl;
in.close();

return 0;
}

Here are the new ones that I did not see it while I learned c++:

#include
#include
using namespace std;
int main()
{
auto sum = [](int a, int b) {
return a + b;
};
cout<<"Enter 2 values with space in between: "; cin>>a>>b;
cout<<endl;
cout <<"Sum of two integers:"<< sum(a, b) << endl;

return 0;
}

#include
#include
using namespace std;

int main()
{
int a,b;

a = (b=1,b+2);
cout<<"Value of x = "<<x;
cout<<endl;

b = (a<5)?0:1;
if(b == 0)
cout<<"a is less than 5"<<endl;
else
cout<<"a is greater than 5"<<endl;

return 0;

}


上一篇
Quick review of C
下一篇
About open source
系列文
Women in IT field30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言